import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { User } from '../../../users/entities/user.entity';

/**
 * 支付渠道枚举
 */
export enum PaymentChannel {
  WECHAT = 'wechat',
  ALIPAY = 'alipay',
  UNIONPAY = 'unionpay',
}

/**
 * 支付状态枚举
 */
export enum PaymentStatus {
  PENDING = 'pending',
  PAID = 'paid',
  FAILED = 'failed',
  CANCELLED = 'cancelled',
}

/**
 * 用户充值记录实体
 */
@Entity({ name: 'user_recharges', comment: '用户充值记录表' })
export class UserRecharge {
  @ApiProperty({ description: '充值记录ID' })
  @PrimaryGeneratedColumn({ comment: '充值记录ID' })
  id: number;

  @ApiProperty({ description: '用户ID' })
  @Column({ comment: '用户ID' })
  userId: number;

  @ApiProperty({ description: '实际充值金额（单位：分）' })
  @Column({ 
    type: 'int',
    comment: '实际充值金额，单位分' 
  })
  amount: number;

  @ApiProperty({ description: '赠送代金券金额（单位：分）' })
  @Column({ 
    type: 'int',
    default: 0,
    comment: '赠送的代金券金额，单位分' 
  })
  bonusVoucher: number;

  @ApiProperty({ description: '支付渠道', enum: PaymentChannel })
  @Column({ 
    type: 'enum',
    enum: PaymentChannel,
    comment: '支付渠道' 
  })
  paymentChannel: PaymentChannel;

  @ApiProperty({ description: '支付状态', enum: PaymentStatus })
  @Column({ 
    type: 'enum',
    enum: PaymentStatus,
    default: PaymentStatus.PENDING,
    comment: '支付状态' 
  })
  paymentStatus: PaymentStatus;

  @ApiProperty({ description: '订单号' })
  @Column({ 
    unique: true,
    length: 64,
    comment: '订单号，唯一标识' 
  })
  orderNo: string;

  @ApiProperty({ description: '第三方支付订单号' })
  @Column({ 
    nullable: true,
    length: 128,
    comment: '第三方支付平台的订单号' 
  })
  thirdPartyOrderNo?: string;

  @ApiProperty({ description: '支付完成时间' })
  @Column({ 
    nullable: true,
    comment: '支付完成时间' 
  })
  paidAt?: Date;

  @ApiProperty({ description: '失败原因' })
  @Column({ 
    nullable: true,
    comment: '支付失败原因' 
  })
  failureReason?: string;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn({ comment: '记录创建时间' })
  createdAt: Date;

  // 关联用户
  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;
}